package com.aifast.system.sys.controller;

import com.aifast.common.annotations.AiIdempotent;
import com.aifast.common.base.Controller;
import com.aifast.common.base.R;
import com.aifast.common.layui.LayuiDataTable;
import com.aifast.system.sys.entity.SysFile;
import com.aifast.system.sys.service.SysFileService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * 文件存储 控制层
 *
 * @author fujian
 * @date 2022-08-31
 */
@RequiredArgsConstructor
@RestController
@RequestMapping("/sys/file")
public class SysFileController extends Controller {

    private final SysFileService service;

    @GetMapping("list")
    public LayuiDataTable list(SysFile sysFile) {
        return service.list(sysFile, getPage());
    }

    @AiIdempotent
    @PostMapping("add")
    public R add(SysFile sysFile) {
        return service.insertOrUpdate(sysFile) ? OK() : ERROR();
    }

    @AiIdempotent
    @GetMapping("del")
    public R del() {
        return service.del(getInt("id"), get("ids")) ? OK() : ERROR();
    }

    @GetMapping("info")
    public R info() {
        return service.info(getInt("id"));
    }


    @PostMapping("upload")
    public R upload(@RequestParam("file") MultipartFile file) throws IOException {
        return service.upload(file, get("tableName"), getInt("tableDataId")) ? OK() : ERROR();
    }

    @PostMapping("simpleUpload")
    public R simpleUpload(@RequestParam("file") MultipartFile file) throws IOException {
        return service.simpleUpload(file,get("tableName"));
    }

    @GetMapping("rename")
    public R rename(){
        return service.rename(getInt("id"),get("name")) ? OK() : ERROR();
    }

    @GetMapping("download")
    public void download() throws IOException {
        service.download(getInt("id"),request,response);
    }


}
